package jsyf.oa.dao.customerReport;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import jsyf.oa.entity.report.mineCustomer.CustomerReportEntity;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;

/**
 * Created by 92852 on 2018/8/11.
 */
@Repository
public class AllCustomerReportDao extends BaseDao {

    public AllCustomerReportDao() {
        super.iBaseEntity = new CustomerReportEntity();
    }

    public List<CustomerReportEntity> getEntityListByCondition(String condition, Object[] params) {
        List<JSONObject> jsonList = getJSONListByCondition(condition, params);
        List<CustomerReportEntity> entityList = new ArrayList<>();
        for (int i = 0; i < jsonList.size(); i++) {
            CustomerReportEntity entity = (CustomerReportEntity) convertJson2Entity(jsonList.get(i));
            entityList.add(entity);
        }
        return entityList;
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, CustomerReportEntity.class);
    }

    public List<JSONObject> getCustomersByUserIds( String condition,int requestPage, int pageSize) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT  * FROM v_sys_user_2_customer\n" +
                "WHERE 1=1\n");
        sql.append(condition);
        super.appendPageSql(sql, requestPage, pageSize);
        List<JSONObject> result = super.queryForJsonList(sql.toString());
        return result;
    }

    public Integer getCountGroupCustomerList(String condition) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT  COUNT(1) AS count  FROM v_sys_user_2_customer\n" +
                "WHERE 1=1\n");
        sql.append(condition);
        String count = super.queryForString(sql.toString());
        return Integer.parseInt(count);
    }
    public List<JSONObject> getOutGroupByUserId(String condition) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT  * FROM v_sys_user_2_customer\n" +
                "WHERE 1=1\n");
        sql.append(condition);
        List<JSONObject> result = super.queryForJsonList(sql.toString());
        return result;
    }

    public List<JSONObject> getProvinceByRegion(String regionIds) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT\n" +
                "provinceName\n" +
                "FROM\n" +
                "v_sys_region_province\n" +
                "WHERE regionId in(" + regionIds + ")");
        List<JSONObject> result = super.queryForJsonList(sql.toString());
        return result;
    }

    public Integer updateCustomerCreateByUserId(String userId, String provinceName) {
        StringBuffer sql = new StringBuffer();
        sql.append("UPDATE t_wrt_customer\n" +
                "SET CREATE_USER_ID='"+userId+"'\n" +
                "WHERE 1=1\n" +
                "AND PROVINCE_NAME in( '"+provinceName+"')\n" +
                "AND CITY_NAME in('"+provinceName+"')\n" +
                "AND COUNTY_NAME in('"+provinceName+"')\n" +
                "AND CREATE_USER_ID in(SELECT  id FROM v_sys_user WHERE jobState=1 AND roleName='区域经理')");
        return this.getJdbcTemplate().update(sql.toString());
    }

    public Integer updateProvinceById(String ids, String provinceName,String cityName,String countyName){

        StringBuffer sql = new StringBuffer();
        sql.append("update t_wrt_customer\n" +
                "SET PROVINCE_NAME='"+provinceName+"',\n" +
                "CITY_NAME='"+cityName+"',\n" +
                "COUNTY_NAME='"+countyName+"'\n" +
                "WHERE ID in("+ids+")\n"+
        "AND PROVINCE_NAME IS NULL\n"
        );
        return this.getJdbcTemplate().update(sql.toString());
    }
    public Integer updateProvinceByIdNoAll(String ids, String provinceName,String cityName,String countyName){

        StringBuffer sql = new StringBuffer();
        sql.append("update t_wrt_customer\n" +
                "SET PROVINCE_NAME='"+provinceName+"',\n" +
                "CITY_NAME='"+cityName+"',\n" +
                "COUNTY_NAME='"+countyName+"'\n" +
                "WHERE ID in("+ids+")\n"
        );
        return this.getJdbcTemplate().update(sql.toString());
    }
    public Integer updateCreateById(String ids, String customerCreateUserId){

        StringBuffer sql = new StringBuffer();
        sql.append("update t_wrt_customer\n" +
                "SET CREATE_USER_ID='"+customerCreateUserId+"'\n" +
                "WHERE ID in("+ids+")");
        return this.getJdbcTemplate().update(sql.toString());
    }

    public  List<JSONObject> getCustomersByUserIdsNoPage(String condition){
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT  customerId  FROM v_sys_user_2_customer\n" +
                "WHERE 1=1\n");
        sql.append(condition);
        List<JSONObject> result = super.queryForJsonList(sql.toString());
        return result;
    }


    public  List<JSONObject> getCustomerIds(String id){
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT  CUSTOMER_ID AS customerId  \n" +
                "FROM t_wrt_customer_visit \n" +
                "WHERE CREATE_USER_ID='"+id+"' \n" +
                "AND CUSTOMER_ID IS NOT NULL  \n");
        List<JSONObject> result = super.queryForJsonList(sql.toString());
        return result;
    }

    public Integer updateCc(String ids, String customerCreateUserId){
        StringBuffer sql = new StringBuffer();
        sql.append("update t_wrt_customer\n" +
                "SET CREATE_USER_ID='"+customerCreateUserId+"'\n" +
                "WHERE ID in("+ids+")");
        return this.getJdbcTemplate().update(sql.toString());
    }
    /*身份证号中间*代替*/
    public static String idCardFormat(String idCard){
        StringBuilder sb = new StringBuilder(idCard);
        sb.replace(3, 15, "************");
        return sb.toString();
    }


}

